﻿//-----------------------------------------------------------------------
// <copyright file="autoselecttextboxfielddefinition.cs" company="Jamie Knight">
// Copyright (c) Jamie Knight. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System.ComponentModel.DataAnnotations;
using System.Windows;
using DevKnightlie.WPF.AutoForm.FieldDefinitions;
using Xceed.Wpf.Toolkit;

namespace DevKnightlie.WPF.AutoForm.WPFToolkit.FieldDefinitions
{
    /// <summary>
    /// Field definition which creates an AutoSelectTextBox control
    /// </summary>
    public class AutoSelectTextBoxFieldDefinition: BaseFieldDefinition
    {
        #region Methods

        public override void BuildInputControl()
        {
            AutoSelectTextBox textBox = new AutoSelectTextBox()
            {
                HorizontalContentAlignment = HorizontalAlignment.Left,
                VerticalContentAlignment = VerticalAlignment.Center
            };
            SetupEnabledBinding(textBox);
            if (PropertyInfo != null)
            {
                // See if there's a MaxLength attribute against the property
                var attrs = (MaxLengthAttribute[])PropertyInfo.GetCustomAttributes(typeof(MaxLengthAttribute), false);
                if (attrs.Length > 0)
                {
                    MaxLengthAttribute attr = attrs[0];
                    textBox.MaxLength = attr.Length;
                }
            } 
            InputControl = ContainerControl = textBox;
        }

        #endregion
    }
}
